package com.bdqn.interceptor;

import com.alibaba.fastjson.JSON;
import com.bdqn.bean.bo.RestResponseBo;
import com.bdqn.bean.bo.TokenPayloadBo;
import com.bdqn.utli.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author 忍
 * @title: TokenInterceptor
 * @projectName Teaching
 * @description: TODO
 * @date 2019/5/218:50
 */
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        String token = request.getHeader("Authorization");
        if (token!=null){
            if (JwtUtil.verify(token)){
                TokenPayloadBo result = JwtUtil.payload(token);
                if (result.getIsSeller() == 1) {
                    request.setAttribute("payload", result);
                    return true;
                }
            }
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(JSON.toJSONString(RestResponseBo.fail("用户认证失败")));
        return false;
    }
}
